package com.be.beadmin.quality.controller;

import com.alibaba.excel.EasyExcel;
import com.be.beadmin.common.entity.PageEntity;
import com.be.beadmin.common.exception.BeadminException;
import com.be.beadmin.common.utils.ResultModel;
import com.be.beadmin.quality.dto.input.TemplateInspectionItemDto;
import com.be.beadmin.quality.dto.output.TemplateInspectionItemEx;
import com.be.beadmin.quality.service.TemplateInspectionItemService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

/**
* @author ljh
* @date 2023-05-18
**/
@Api(tags = {"检验表模板检验项"})
@RestController
public class TemplateInspectionItemController {

    @Autowired
    private  TemplateInspectionItemService templateInspectionItemService;

    @ApiOperation("分页获取所有检验项")
    @PostMapping(value = "/quality/getTemplateInspectionItemByPage")
    public ResultModel getTemplateInspectionItemByPage(@RequestBody PageEntity pageEntity)  {
        return ResultModel.success(templateInspectionItemService.getTemplateInspectionItemByPage(pageEntity)) ;
    }

    @ApiOperation("（可用于排序序号判重）获取指定排序序号的检验项")
    @PostMapping(value = "/quality/getTemplateInspectionItemBySortId")
    public ResultModel getTemplateInspectionItemBySortId(
            @ApiParam(value = "检验表模板Id") @RequestParam String inspectionTemplateId,
            @ApiParam(value = "排序序号") @RequestParam int sortId)  {
        try {
            return ResultModel.success(templateInspectionItemService.getTemplateInspectionItemBySortId(inspectionTemplateId,sortId)) ;
        } catch (BeadminException e) {
            return ResultModel.error(-1,e.getMessage());
        } catch (Exception e) {
            return ResultModel.error(-2,e.getMessage());
        }
    }

    @ApiOperation("获取检验表模板id下的所有检验项(不分页)")
    @PostMapping(value = "/quality/getTemplateInspectionItem")
    public ResultModel getTemplateInspectionItem(@ApiParam(value = "检验表模板Id") @RequestParam String inspectionTemplateId)  {
        List<TemplateInspectionItemDto> templateInspectionItemDtoList = templateInspectionItemService.getTemplateInspectionItem(inspectionTemplateId);
        return ResultModel.success(templateInspectionItemDtoList) ;
    }

    @ApiOperation("新增检验表模板检验项")
    @PostMapping("/quality/addTemplateInspectionItem")
    public ResultModel addTemplateInspectionItem(@RequestBody TemplateInspectionItemDto templateInspectionItemDto){
        templateInspectionItemService.addTemplateInspectionItem(templateInspectionItemDto);
        return ResultModel.success(ResultModel.ADD);
    }

    @PostMapping("/quality/delTemplateInspectionItem")
    public ResultModel delTemplateInspectionItem(@RequestBody TemplateInspectionItemDto templateInspectionItemDto){
        templateInspectionItemService.delTemplateInspectionItem(templateInspectionItemDto.getId());
        return ResultModel.success(ResultModel.DELETE);
    }

    @ApiOperation("编辑检验表模板检验项")
    @PostMapping("/quality/updateTemplateInspectionItem")
    public ResultModel updateTemplateInspectionItem(@RequestBody TemplateInspectionItemDto templateInspectionItemDto){
        templateInspectionItemService.updateTemplateInspectionItem(templateInspectionItemDto);
        return ResultModel.success(ResultModel.UPDATE);
    }


    @PostMapping("/quality/exportTemplateInspectionItem")
    public void exportTemplateInspectionItem(HttpServletResponse response ,@RequestBody PageEntity pageEntity) throws IOException {
        Page<TemplateInspectionItemEx> templateInspectionItemPage = templateInspectionItemService.getTemplateInspectionItemByPage(pageEntity);
        List<TemplateInspectionItemEx> templateInspectionItemExes = templateInspectionItemPage.getContent();
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("TemplateInspectionItem接口", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), TemplateInspectionItemEx.class).sheet("sheet1").doWrite(templateInspectionItemExes);
    }
}